﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace CPGE
{
    public partial class Diaporama : System.Web.UI.UserControl
    {
        public string Title { get; set; }
        public bool Loop { get; set; }
        public bool AutoSlide { get; set; }
        public bool Pagination { get; set; }
        public int AutoSlideInterval { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string ImagePath { get; set; }
        public bool WithLink =false;

        public string ImageListHtml
        {
            get
            {
                var html = new StringBuilder();
                if (!string.IsNullOrEmpty(ImagePath))
                {
                    var folder = new DirectoryInfo(Server.MapPath(ImagePath));
                    var files = folder.GetFiles();
                    foreach (var file in files)
                    {
                        if (WithLink)
                            html.AppendFormat("<li><a href='{0}' target='_blank'><img  border='0' src='{0}' width='{1}' height='{2}' /></a></li>",
                                          ConvertPhysicalPathToRelative(file.FullName),
                                          Width,
                                          Height);
                        else
                            html.AppendFormat("<li><img  src='{0}' width='{1}' height='{2}' /></li>",
                                          ConvertPhysicalPathToRelative(file.FullName),
                                          Width,
                                          Height);
                    }
                }
                return html.ToString();
            }
        }

        protected string ConvertPhysicalPathToRelative(string path)
        {
            return path.Remove(0, Request.PhysicalApplicationPath.Length).Replace("\\", "/");
        }

        public Diaporama()
        {
            Title = "Diaporama";
            Loop = false;
            AutoSlide = false;
            Pagination = false;
            AutoSlideInterval = 3000;
            Width = 400;
            Height = 300;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.RegisterClientScriptBlock("Jquery","<script type='text/javascript' src='js/jquery-1.3.2.js'></script>");
            Page.RegisterClientScriptBlock("Zoomer", "<script type='text/javascript' src='js/ddpowerzoomer.js'></script>");
            Page.RegisterClientScriptBlock("Carousel", "<script type='text/javascript' src='js/jquery.carousel.min.js'></script>");

            Page.Header.Controls.Add(new Literal() { Text = "<link type='text/css' rel='stylesheet' href='Diaporama.css' />" });
        }
    }
}